#include<pthread.h>
#include<unistd.h>
#include<stdio.h>

pthread_mutex_t g_mutex;
pthread_cond_t g_cond;

void* thread_fun(void* arg)
{
        pthread_mutex_lock(&g_mutex);
        pthread_cond_wait(&g_cond, &g_mutex);

        printf("cv get.\n");
        //延迟1s再释放lock给下一个线程使用
        sleep(1);

        pthread_mutex_unlock(&g_mutex);
}

int main(void)
{
        pthread_t t[2];
        int i;

        pthread_mutex_init( &g_mutex, NULL );
        pthread_cond_init( &g_cond, NULL );

        for (i = 0; i < 2; i++)
        {
                pthread_create(&t[i], NULL, thread_fun, NULL);
        }
        sleep(5);

        pthread_mutex_lock(&g_mutex);
        pthread_cond_broadcast(&g_cond);
        pthread_mutex_unlock(&g_mutex);

        for (i = 0; i < 2; i++)
        {
                pthread_join(t[i], NULL);
        }

        pthread_mutex_destroy(&g_mutex);
        pthread_cond_destroy(&g_cond);
}
